﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data;
using UniMana.Utils.Data;

namespace UniMana.Data
{
    public static class Training_Subject_ClassData
    {
        #region [Chi]
        /// <summary>
        /// Lấy danh sách lớp học phần
        /// </summary>
        /// <param name="nTable">Table Training_Subject_Class</param>
        /// <param name="termKey">Mã học kì</param>
        /// <param name="schoolYearKey">Mã năm học</param>
        /// <returns></returns>
        public static IQueryable<Training_Subject_Class> GetList(this Table<Training_Subject_Class> nTable, int termKey, int schoolYearKey)
        {

            var SubjectClassList = from classList in nTable
                                   where classList.Catalogue_TermKey == termKey
                                   where classList.Catalogue_SchoolYearKey == schoolYearKey
                                   select classList;
            return SubjectClassList;
        }
        /// <summary>
        /// Lấy danh sách lớp học phần theo học kì, năm học, khoa
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="t3"></param>
        /// <param name="termKey">Key học kì</param>
        /// <param name="schoolYearKey">Key năm học</param>
        /// <param name="deparment">Key khoa</param>
        /// <returns></returns>
        public static IQueryable GetSubjectClassList(this Table<Training_Subject_Class> t1,  Table<Training_Subject> t2, 
            Table<Catalogue_Department> t3,int termKey, int schoolYearKey, int deparment)
        {
            var l = from subjectClass in t1
                    join
                        subject in t2 on subjectClass.Training_SubjectKey equals subject.Training_SubjectKey
                    join
                        depart in t3 on subject.Catalogue_DepartmentKey equals depart.Catalogue_DepartmentKey
                    where subjectClass.Catalogue_TermKey == termKey
                    where subjectClass.Catalogue_SchoolYearKey == schoolYearKey
                    where depart.Catalogue_DepartmentKey == deparment
                    select subjectClass;
            return l;
        }
        /// <summary>
        /// Lấy danh sách lớp học phần gần giống với ID nhập vào
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="subjectClassID">ID lớp học phần</param>
        /// <returns></returns>
        public static IQueryable GetLikeSubjectClassList(this Table<Training_Subject_Class> t1, string subjectClassID)
        {
            var sl = from s in t1
                     where s.ID.Contains(subjectClassID)
                     select new
                     {
                         s.Training_Subject_ClassKey,
                         s.ID,
                         s.Name
                     };
            return sl;
        }
        /// <summary>
        /// Lấy danh sách lớp học phần gần giống với ID nhập vào & chưa nhập điểm
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="subjectClassID"></param>
        /// <returns></returns>
        public static IQueryable GetLikeSubjectClassList(this Table<Training_Subject_Class> t1,
                                                    Table<Training_SubjectClass_ScoreType> t2,
                                                                        string subjectClassID)
        {
            var sl = (from s in t1 join 
                          ss in t2 on s.Training_Subject_ClassKey equals ss.Training_Subject_ClassKey
                     where s.ID.Contains(subjectClassID)
                     //where ( ss.Lock == false || ss.Lock == null)
                     select new
                     {
                         s.Training_Subject_ClassKey,
                         s.ID,
                         s.Name
                     }).Distinct();
            return sl;
        }
        /// <summary>
        /// Lấy danh sách lớp học phần theo học kì, năm học, và chưa nhập điểm
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="termKey">Key học kì</param>
        /// <param name="schoolYearKey">Key năm học</param>
        /// <returns></returns>
        public static IQueryable GetSubjectClassListByTermSchoolYear(this Table<Training_Subject_Class> t1,
                                                                       Table<Training_SubjectClass_ScoreType> t2,
                                                                        int termKey=-1, int schoolYearKey=-1)
        {
            var sl = (from s in t1 join
                          ss in t2 on s.Training_Subject_ClassKey equals ss.Training_Subject_ClassKey
                     //where s.Catalogue_SchoolYearKey == schoolYearKey
                     //where s.Catalogue_TermKey == termKey
                     //where (ss.Lock == false || ss.Lock == null)
                     select new
                     {
                         s.Training_Subject_ClassKey,
                         s.ID,
                         s.Name,
                         s.Catalogue_TermKey,
                         s.Catalogue_SchoolYearKey
                     }).Distinct();
            if (termKey != -1)
            {
                sl = sl.Where(t => t.Catalogue_TermKey == termKey);
            }
            if (schoolYearKey != -1)
            {
               sl = sl.Where(s => s.Catalogue_SchoolYearKey == schoolYearKey);
            }
            return sl;
        }
        /// <summary>
        /// Lấy danh sách lớp học phần theo học kì, năm học đê xem tình trạng nhập và kiểm tra nhập điểm
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="termKey">Key học kì</param>
        /// <param name="schoolYearKey">Key năm học</param>
        /// <returns></returns>
        public static IQueryable GetSubjectClassList(this Table<Training_Subject_Class> t1, 
                                                     Table<Training_Student_Subject_Class> t2,
                                                     int termKey, int schoolYearKey)
        {
            var l = from a in t1
                    join c in
                        (
                            (from b in t2
                             group b by new
                             {
                                 b.Training_Subject_ClassKey
                             } into g
                             select new
                             {
                                 Training_Subject_ClassKey = (System.Int32?)g.Key.Training_Subject_ClassKey,
                                 SL = (Int64?)g.Count()
                             })) on new { Training_Subject_ClassKey = a.Training_Subject_ClassKey } equals new { Training_Subject_ClassKey = Convert.ToInt32(c.Training_Subject_ClassKey) }
                    where
                      a.Catalogue_SchoolYearKey == schoolYearKey &&
                      a.Catalogue_TermKey == termKey
                    select new
                    {
                        Training_Subject_ClassKey = (System.Int32?)a.Training_Subject_ClassKey,
                        a.Training_SubjectKey,
                        a.Yearly,
                        a.ID,
                        a.Name,
                        c.SL,
                        StatusInputScore = a.StatusInputScore == true? "Xong":"Chưa xong",
                        CheckInputScore = a.CheckInputScore == true ? "Xong" : "Chưa xong"
                    };
            return l;
        }
        /// <summary>
        /// Lấy danh sách lớp học phần theo học kì, năm học để xem tình trạng nhập
        ///  và kiểm tra theo từng loại điểm
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="t3"></param>
        /// <param name="nPersonnel_Personnel"></param>
        /// <param name="termKey">Key học kì</param>
        /// <param name="schoolYearKey">Key năm học</param>
        /// <param name="scoreTypeKey">Key loại điểm</param>
        /// <returns></returns>
        public static IQueryable GetSubjectClassList(this Table<Training_Subject_Class> t1, 
                                                     Table<Training_SubjectClass_ScoreType> t2, 
                                                        Table<Training_ScoreType> t3,
                                                    Table<Personnel_Personnel> nPersonnel_Personnel,
                                                     int termKey, int schoolYearKey, int scoreTypeKey)
        {
            //string strName = new System_UserInfo(UniMana.Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey).Properties.FullName.ToString();
            var s =( from sc in t1
                    join
                        ss in t2 on sc.Training_Subject_ClassKey equals ss.Training_Subject_ClassKey
                    join
                        st in t3 on ss.Training_ScoreTypeKey equals st.Training_ScoreTypeKey
                    where sc.Catalogue_TermKey == termKey
                    where sc.Catalogue_SchoolYearKey == schoolYearKey
                    where ss.Training_ScoreTypeKey == scoreTypeKey
                    select new
                    {
                        sc.Training_Subject_ClassKey,
                        sc.ID,
                        sc.Name,
                        Lock = ss.Lock ==true? "Xong":"Chưa xong",
                        Check = ss.Check == true? "Xong":"Chưa xong",
                        LockPersonnel_PersonnelKey = ss.LockPersonnel_PersonnelKey== null ? "" :  ((from pp in nPersonnel_Personnel where pp.Personnel_PersonnelKey == ss.LockPersonnel_PersonnelKey select pp.LastName).FirstOrDefault() +" "+ (from pp in nPersonnel_Personnel where pp.Personnel_PersonnelKey == ss.LockPersonnel_PersonnelKey select pp.Name).FirstOrDefault()),
                        CheckPersonnel_PersonnelKeyKey = ss.CheckPersonnel_PersonnelKeyKey == null ? "" : ((from pp1 in nPersonnel_Personnel where pp1.Personnel_PersonnelKey == ss.CheckPersonnel_PersonnelKeyKey select pp1.LastName).FirstOrDefault() + " " + (from pp1 in nPersonnel_Personnel where pp1.Personnel_PersonnelKey == ss.CheckPersonnel_PersonnelKeyKey select pp1.Name).FirstOrDefault())
                    }).Distinct();
            return s;
        }
        #endregion

        #region [Thảo]
        /// <summary>
        /// thống kê chọn môn học
        /// </summary>
        /// <param name="cataSchoolYear"></param>
        /// <param name="cataTerm"></param>
        /// <param name="traSubject_Class"></param>
        /// <param name="traSub"></param>
        /// <param name="_iSchoolYear"></param>
        /// 
        /// <param name="_iCata_Term"></param>
        /// <returns></returns>
        public static IQueryable TrainingSubject_Class_Sub(this Table<Training_Subject_Class> traSubject_Class,
                      Table<Catalogue_Department> cataDe,
                      Table<Catalogue_Term> cataTerm,
                      Table<Training_Subject> traSub,
                      Table<Catalogue_SchoolYear> cataSchoYea,
                      int _iCataTerm, int _iCataScholYea)
        {
            var view1 = from t in traSubject_Class
                        join t0 in traSub on new { Training_SubjectKey = Convert.ToInt32(t.Training_SubjectKey) } equals new { Training_SubjectKey = t0.Training_SubjectKey }
                        join t1 in cataDe on new { Catalogue_DepartmentKey = Convert.ToInt32(t0.Catalogue_DepartmentKey) } equals new { Catalogue_DepartmentKey = t1.Catalogue_DepartmentKey }
                        join t2 in cataTerm on new { Catalogue_TermKey = Convert.ToInt32(t.Catalogue_TermKey) } equals new { Catalogue_TermKey = t2.Catalogue_TermKey }
                        join t3 in cataSchoYea on new { Catalogue_SchoolYearKey = Convert.ToInt32(t.Catalogue_SchoolYearKey) } equals new { Catalogue_SchoolYearKey = t3.Catalogue_SchoolYearKey }
                        group new { t3, t2, t1, t0 } by new
                        {
                            t3.Catalogue_SchoolYearKey,
                            t3.Name,
                            t2.Catalogue_TermKey,
                            Column1 = t2.Name,
                            t1.Catalogue_DepartmentKey,
                            Column2 = t1.Name,
                            t0.Training_SubjectKey,
                            t0.ID,
                            Column3 = t0.Name
                        } into g
                        where g.Key.Catalogue_SchoolYearKey == _iCataScholYea &&
                          g.Key.Catalogue_TermKey == _iCataTerm

                        select new
                        {
                            Catalogue_SchoolYearKey = (System.Int32?)g.Key.Catalogue_SchoolYearKey,
                            TenNamHoc = g.Key.Name,
                            Catalogue_TermKey = (System.Int32?)g.Key.Catalogue_TermKey,
                            TenHocKy = g.Key.Column1,
                            Catalogue_DepartmentKey = (System.Int32?)g.Key.Catalogue_DepartmentKey,
                            TenKhoa = g.Key.Column2,
                            Training_SubjectKey = (System.Int32?)g.Key.Training_SubjectKey,
                            g.Key.ID,
                            TenMonHoc = g.Key.Column3,
                            TongSoLopHP = (Int64?)g.Count()
                        };
            return view1.Distinct();
        }
        /// <summary>
        /// thống kê khi ko cần chọn môn học
        /// </summary>
        /// <param name="cataSchoolYear"></param>
        /// <param name="cataTerm"></param>
        /// <param name="traSubject_Class"></param>
        /// <param name="cataDep"></param>
        /// <param name="traSub"></param>
        /// <param name="student"></param>
        /// <param name="_iSchoolYear"></param>
        /// <param name="_iCata_Term"></param>
        /// <param name="_iTraSub"></param>
        /// <param name="_iCata_Dep"></param>
        /// <returns></returns>
        public static IQueryable TrainingSubject_Class_NoSub(this   Table<Training_TrainingProgram> traPro,
                      Table<Training_Subject_Class> traSubject_Class,
                      Table<Training_TrainingProgram_Detail> traDe,
                      Table<Catalogue_Term> cataTerm,
                      Table<Training_Subject> traSub,
                      int _iTraPro, int _iCataTerm)
        {
            var view2 = from t in traSubject_Class
                        join t0 in traSub on new { Training_SubjectKey = Convert.ToInt32(t.Training_SubjectKey) } equals new { Training_SubjectKey = t0.Training_SubjectKey }
                        join t1 in cataTerm on new { Catalogue_TermKey = Convert.ToInt32(t.Catalogue_TermKey) } equals new { Catalogue_TermKey = t1.Catalogue_TermKey }
                        join t2 in traDe
                              on new { Training_SubjectKey = Convert.ToInt32(t.Training_SubjectKey), Column1 = t0.Training_SubjectKey }
                          equals new { Training_SubjectKey = t2.Training_SubjectKey, Column1 = t2.Training_SubjectKey }
                        join t3 in traPro on t2.Training_TrainingProgramKey equals t3.Training_TrainingProgramKey
                        group new { t1, t0, t3 } by new
                        {
                            t1.Catalogue_TermKey,
                            t1.Name,
                            t0.Training_SubjectKey,
                            t0.ID,
                            Column1 = t0.Name,
                            t3.Training_TrainingProgramKey,
                            Column2 = t3.Name
                        } into g
                        where g.Key.Catalogue_TermKey == _iCataTerm &&
                          g.Key.Training_TrainingProgramKey == _iTraPro

                        select new
                        {
                            Training_TrainingProgramKey = (System.Int32?)g.Key.Training_TrainingProgramKey,
                            g.Key.Column2,
                            Catalogue_TermKey = (System.Int32?)g.Key.Catalogue_TermKey,
                            TenHocKy = g.Key.Name,
                            Training_SubjectKey = (System.Int32?)g.Key.Training_SubjectKey,
                            g.Key.ID,
                            TenMonHoc = g.Key.Column1,
                            TongSoLopHP = (Int64?)g.Count()
                        };
            return view2;
        }
        #endregion

        #region [Chi]
        /// <summary>
        /// lấy danh sách lớp học phần theo môn, năm học, học kỳ
        /// </summary>
        /// <param name="nTable"></param>
        /// <param name="SubjectKey"></param>
        /// <param name="SchoolYearKey"></param>
        /// <param name="TermKey"></param>
        /// <returns></returns>
        public static IQueryable<Training_Subject_Class>
            GetListBySubjectKeyandSchoolYearKeyandTermKey(this Table<Training_Subject_Class> nTable,
                                                            int SubjectKey, int SchoolYearKey, int TermKey)
        {
            var Subject_Class = nTable.Where(p => p.Training_SubjectKey == SubjectKey
                                                        && p.Catalogue_SchoolYearKey == SchoolYearKey
                                                        && p.Catalogue_TermKey == TermKey);
            return Subject_Class.Distinct();
        }
        /// <summary>
        /// lấy danh sách lớp học phần theo môn, năm học, học kỳ, trạng thái mở
        /// </summary>
        /// <param name="nTable"></param>
        /// <param name="SubjectKey"></param>
        /// <param name="SchoolYearKey"></param>
        /// <param name="TermKey"></param>
        /// <param name="Opened"></param>
        /// <returns></returns>
        public static IQueryable<Training_Subject_Class>
             GetListBySubjectKey_SchoolYearKey_TermKey_Opened(this Table<Training_Subject_Class> nTable,
                                                                int SubjectKey, int SchoolYearKey, int TermKey,
                                                                bool Opened)
        {
            var Subject_Class = nTable.Where(p => p.Training_SubjectKey == SubjectKey
                                                        && p.Catalogue_SchoolYearKey == SchoolYearKey
                                                        && p.Catalogue_TermKey == TermKey
                                                        && p.Opened == Opened);
            return Subject_Class.Distinct();
        }
        /// <summary>
        /// Danh sách xếp theo số học sinh của lớp học phần theo năm học,học kỳ, môn
        /// </summary>
        /// <param name="training_Subject_Class"></param>
        /// <param name="training_Student_Subject_Class"></param>
        /// <param name="SubjectKey"></param>
        /// <param name="SchoolYearKey"></param>
        /// <param name="TermKey"></param>
        /// <returns></returns>
        public static IQueryable
            GetBySubjectKey_SchoolYearKey_TermKey_sort(this Table<Training_Subject_Class> training_Subject_Class,
                                                                Table<Training_Student_Subject_Class> training_Student_Subject_Class,
                                                                int SubjectKey, int SchoolYearKey, int TermKey)
        {
            var Subject_Class = from sj_c in training_Subject_Class
                                join st_sj_c in training_Student_Subject_Class on sj_c.Training_Subject_ClassKey
                                    equals st_sj_c.Training_Subject_ClassKey into st_sj_c_join
                                from training_student_subject_classes in st_sj_c_join.DefaultIfEmpty()
                                group sj_c by new
                                {
                                    sj_c.ID,
                                    sj_c.Training_Subject_ClassKey,
                                    sj_c.Name,
                                    sj_c.Training_SubjectKey,
                                    sj_c.Catalogue_TermKey,
                                    sj_c.Catalogue_SchoolYearKey,
                                    sj_c.Formular,
                                    sj_c.Personnel_PersonnelKey,
                                    sj_c.Opened
                                } into g
                                orderby
                                  g.Count() descending
                                where g.Key.Training_SubjectKey == SubjectKey
                                                        && g.Key.Catalogue_SchoolYearKey == SchoolYearKey
                                                        && g.Key.Catalogue_TermKey == TermKey
                                                        && g.Key.Opened == true
                                select new
                                {
                                    Training_Subject_ClassKey = (System.Int32?)g.Key.Training_Subject_ClassKey,
                                    g.Key.ID,
                                    g.Key.Name,
                                    Training_SubjectKey = (System.Int32?)g.Key.Training_SubjectKey,
                                    Catalogue_TermKey = (System.Int32?)g.Key.Catalogue_TermKey,
                                    Catalogue_SchoolYearKey = (System.Int32?)g.Key.Catalogue_SchoolYearKey,
                                    g.Key.Formular,
                                    g.Key.Personnel_PersonnelKey,
                                    Opened = (System.Boolean?)g.Key.Opened
                                };
            return Subject_Class;
        }
        public static IQueryable
            GetwithDepartmentsName_BySchoolYearKeyandTermKey(this Table<Training_Subject_Class> training_Subject_Class,
                                                                Table<Training_Subject> training_Subject,
                                                                Table<Catalogue_Department> catalogue_Department,
                                                                int SchoolYearKey, int TermKey)
        {
            var SJ_C = from sj in training_Subject
                       join sj_c in training_Subject_Class on sj.Training_SubjectKey equals sj_c.Training_SubjectKey
                       join db in catalogue_Department on sj.Catalogue_DepartmentKey equals db.Catalogue_DepartmentKey
                       where sj_c.Catalogue_SchoolYearKey == SchoolYearKey &&
                       sj_c.Catalogue_TermKey == TermKey
                       select new
                       {
                           sj_c.Catalogue_SchoolYearKey,
                           sj_c.Catalogue_TermKey,
                           sj_c.Formular,
                           sj_c.ID,
                           sj_c.Name,
                           sj_c.Opened,
                           sj_c.Personnel_PersonnelKey,
                           sj_c.Training_Subject_ClassKey,
                           sj_c.Training_SubjectKey,
                           Catalogue_DepartmentName = db.Name
                       };
            return SJ_C;
        }
        /// <summary>
        /// lấy danh sách lớp học phần theo ID
        /// </summary>
        /// <param name="training_Subject_Class"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static IQueryable
            GetByID(this Table<Training_Subject_Class> training_Subject_Class,
            string ID)
        {
            var IQuery = training_Subject_Class.Where(q => q.ID == ID);
            return IQuery;
        }
        /// <summary>
        /// thêm loại điểm cho lớp học phần
        /// </summary>
        /// <param name="subject_Class"></param>
        /// <param name="schoolYearKey"></param>
        /// <param name="termKey"></param>
        /// <param name="subjectKey"></param>
        /// <param name="yearly"></param>
        public static void
            AddSubject_Class_Score_Type(this Table<Training_Subject_Class> subject_Class,
            int schoolYearKey = 0,
            int termKey = 0,
            int subjectKey = 0,
            bool yearly = false)
        {
            var Query = (from sj_c in subject_Class
                         where sj_c.Catalogue_SchoolYearKey == schoolYearKey &&
                         sj_c.Catalogue_TermKey == termKey &&
                         sj_c.Training_SubjectKey == subjectKey
                         orderby sj_c.Training_Subject_ClassKey descending
                         select sj_c).Take(1);
            if (Query.Count() > 0)
            {
                foreach (var item in Query)
                {
                    Training_Subject_Class_ScoreTypeInfo info =
                        new Training_Subject_Class_ScoreTypeInfo(
                                    item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_1);
                    info.Properties.Training_Subject_ClassKey = item.Training_Subject_ClassKey;
                    info.Properties.Training_ScoreTypeKey = UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_1;
                    if (yearly)
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.MiddleTestInYearly;
                    }
                    else
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.MiddleTestInNoYearly;
                    }
                    info.Save();

                    info = new Training_Subject_Class_ScoreTypeInfo(
                                    item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_2);
                    info.Properties.Training_Subject_ClassKey = item.Training_Subject_ClassKey;
                    info.Properties.Training_ScoreTypeKey = UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_2;
                    if (yearly)
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.FinalTestInYearly;
                    }
                    else
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.FinalTestInNoYearly;
                    }
                    info.Save();

                    if (yearly)
                    {
                        info = new Training_Subject_Class_ScoreTypeInfo(
                                        item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_3);
                        info.Properties.Training_Subject_ClassKey = item.Training_Subject_ClassKey;
                        info.Properties.Training_ScoreTypeKey = UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_3;
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.FinalTestInYearly;
                        info.Save();
                    }
                    else
                    {
                        info = new Training_Subject_Class_ScoreTypeInfo(
                                        item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_3);
                        info.Delete();
                    }
                }
            }
        }
        /// <summary>
        /// xóa loại điểm lớp học phần
        /// </summary>
        /// <param name="subject_Class"></param>
        /// <param name="schoolYearKey"></param>
        /// <param name="termKey"></param>
        /// <param name="subjectKey"></param>
        /// <param name="yearly"></param>
        public static void
            DeleteSubject_Class_Score_Type(this Table<Training_Subject_Class> subject_Class,
            int subject_ClassKey = 0)
        {
            Training_Subject_Class_ScoreTypeInfo info =
                new Training_Subject_Class_ScoreTypeInfo(
                            subject_ClassKey,
                            UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_1);
            if (info != null)
            {
                info.Delete();
            }

            info = new Training_Subject_Class_ScoreTypeInfo(
                            subject_ClassKey,
                            UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_2);
            if (info != null)
            {
                info.Delete();
            }

            info = new Training_Subject_Class_ScoreTypeInfo(
                            subject_ClassKey,
                            UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_3);
            if (info != null)
            {
                info.Delete();
            }
        }
        /// <summary>
        /// Cập Nhật loại điểm lớp học phần
        /// </summary>
        /// <param name="subject_Class"></param>
        /// <param name="schoolYearKey"></param>
        /// <param name="termKey"></param>
        /// <param name="subjectKey"></param>
        /// <param name="yearly"></param>
        public static void
            SetSubject_Class_Score_Type(this Table<Training_Subject_Class> subject_Class,
            int subject_ClassKey = 0,
            bool yearly = false)
        {
            var Query = (from sjc in subject_Class
                         where sjc.Training_Subject_ClassKey == subject_ClassKey
                         select sjc).Take(1);
            if (Query.Count() > 0)
            {
                foreach (var item in Query)
                {
                    Training_Subject_Class_ScoreTypeInfo info =
                        new Training_Subject_Class_ScoreTypeInfo(
                                    item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_1);
                    info.Properties.Training_Subject_ClassKey = item.Training_Subject_ClassKey;
                    info.Properties.Training_ScoreTypeKey = UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_1;
                    if (yearly)
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.MiddleTestInYearly;
                    }
                    else
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.MiddleTestInNoYearly;
                    }
                    info.Save();

                    info = new Training_Subject_Class_ScoreTypeInfo(
                                    item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_2);
                    info.Properties.Training_Subject_ClassKey = item.Training_Subject_ClassKey;
                    info.Properties.Training_ScoreTypeKey = UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_2;
                    if (yearly)
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.FinalTestInYearly;
                    }
                    else
                    {
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.FinalTestInNoYearly;
                    }
                    info.Save();

                    if (yearly)
                    {
                        info = new Training_Subject_Class_ScoreTypeInfo(
                                        item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_3);
                        info.Properties.Training_Subject_ClassKey = item.Training_Subject_ClassKey;
                        info.Properties.Training_ScoreTypeKey = UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_3;
                        info.Properties.DefaultPercent = UniMana.Utils.Configs.DefaultConfig.FinalTestInYearly;
                        info.Save();
                    }
                    else
                    {
                        info = new Training_Subject_Class_ScoreTypeInfo(
                                        item.Training_Subject_ClassKey, UniMana.Utils.Configs.DefaultConfig.SCORE_TYPE_3);
                        info.Delete();
                    }
                }
            }
        }
        #endregion

        #region [An]
        /// <summary>
        /// Lấy mã lớp học phần
        /// </summary>
        /// <param name="subcls"></param>
        /// <returns></returns>

        public static IQueryable GetClassKeyID(this Table<Training_Subject_Class> nTraining_Subject_Class)
        {
            var value = from s in nTraining_Subject_Class
                        where s.ID != null
                        select new
                        {
                            s.Training_Subject_ClassKey,
                            s.ID
                        };
            return value.Distinct();
        }
        #endregion
    }
}
